﻿using Bll;
using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Myproject.BSM.Ashx.PcBase
{
    /// <summary>
    /// NavigationAjax 的摘要说明
    /// </summary>
    public class NavigationAjax : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (!string.IsNullOrEmpty(cookieHelp.AdminId))
            {
                if (new AdminBLL().ExistsByAdminName(cookieHelp.AdminName) && new AdminBLL().ExistsByAdminId(cookieHelp.AdminId))
                {

                    string dos = context.Request.Params["dos"];
                    string id;
                    #region swith do
                    switch (dos)
                    {
                        case "delete":
                            id = context.Request.QueryString["id"];
                            DataSet DS = new NavBLL().GetList(" ParentID=" + id);
                            if (DS.Tables[0].Rows.Count > 0)
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "删除导航失败,有子导航！", "pc_base_Navigation", "", "", false, "", ""));

                            }
                            else
                            {
                                if (new NavBLL().Delete(int.Parse(id)))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "删除导航成功！", "pc_base_Navigation", "", "", false, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "删除导航失败！", "pc_base_Navigation", "", "", false, "", ""));
                                }
                            }
                            break;

                        case "rank":
                            id = context.Request.QueryString["id"];
                            string rank = context.Request.QueryString["rank"];
                            if (id != "0")
                            {
                                NavModel model = new NavBLL().GetModel(int.Parse(id));
                                model.FatherRank += int.Parse(rank);
                                if (new NavBLL().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改排序成功！", "pc_base_Navigation", "", "", false, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "pc_base_Navigation", "", "", false, "", ""));
                                }
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "pc_base_Navigation", "", "", false, "", ""));
                            }
                            break;

                        case "rank2":
                            id = context.Request.QueryString["id"];
                            string rank2 = context.Request.QueryString["rank"];
                            if (id != "0")
                            {
                                NavModel model = new NavBLL().GetModel(int.Parse(id));
                                model.SonRank += int.Parse(rank2);
                                if (new NavBLL().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改排序成功！", "pc_base_Navigation", "", "", false, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "pc_base_Navigation", "", "", false, "", ""));
                                }
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "pc_base_Navigation", "", "", false, "", ""));
                            }
                            break;

                        case "edit":
                            id = context.Request.Form["pc_base_navEdit_navID"];
                            string name = context.Request.Form["pc_base_navEdit_name"];
                            string ename = context.Request.Form["pc_base_navEdit_ename"];
                            string url = context.Request.Form["pc_base_navEdit_url"];
                            string eurl = context.Request.Form["pc_base_navEdit_eurl"];
                            string rank3 = context.Request.Form["pc_base_navEdit_frank"];
                            string rank22 = context.Request.Form["pc_base_navEdit_srank"];
                            string pid = context.Request.Form["pc_base_navEdit_parentID"];
                            if (id == "0")
                            {
                                NavModel model = new NavModel();
                                model.Title = name;
                                model.ETitle = ename;
                                model.Url = url;
                                model.EUrl = eurl;
                                model.FatherRank = int.Parse(rank3);
                                model.SonRank = int.Parse(rank22);
                                model.ParentID = int.Parse(pid);
                                if (new NavBLL().Add(model) > 0)
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "添加导航成功！", "pc_base_Navigation", "", "", true, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "添加导航失败！", "pc_base_Navigation", "", "", true, "", ""));
                                }
                            }
                            else
                            {
                                NavModel model = new NavBLL().GetModel(int.Parse(id));
                                model.Title = name;
                                model.ETitle = ename;
                                model.Url = url;
                                model.EUrl = eurl;
                                model.FatherRank = int.Parse(rank3);
                                model.SonRank = int.Parse(rank22);
                                model.ParentID = int.Parse(pid);
                                if (new NavBLL().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改导航成功！", "pc_base_Navigation", "", "", true, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改导航失败！", "pc_base_Navigation", "", "", true, "", ""));
                                }
                            }
                            break;
                    }
                    #endregion
                }
                else
                {
                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(301, "登陆超时！", "", "", "", false, "/bsm/logFrom.aspx", "登陆超时！"));
                }
            }
            else
            {
                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(301, "登陆超时！", "", "", "", false, "/bsm/logFrom.aspx", "登陆超时！"));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}